package cc.vv.btpartyjob.activity

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import cc.vv.baselibrary.activity.BaseActivity
import cc.vv.baselibrary.bean.PublicViewHolder
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.global.UserInfoSharePreKey
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.btpartyjob.R
import cc.vv.btpartyjob.activity.login.LoginActivity
import cc.vv.btpartyjob.module.resp.LoginResponseObj


/**
 * 欢迎界面
 * Created by sun.li on 2018/9/11.
 * @author sun.li
 */
class WelcomeActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= 21) {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.statusBarColor = Color.TRANSPARENT
        }
    }


    override fun initLayoutRes(): Int {
        return R.layout.activity_welcome
    }

    override fun initViewHolderObject(): PublicViewHolder? {
        return null
    }

    override fun baseOnClick(viewId: Int) {
    }

    override fun initView(bundle: Bundle?) {
        super.initView(bundle)


        Handler().postDelayed({
            kotlin.run {
                intentActivity()
            }
        }, 1000)
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is LoginResponseObj -> {
                val userInfo = obj.data
                if (null != userInfo) {
                    UserInfoManageUtil.saveUserInfo(userInfo)
                }
            }
        }
    }


    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        //设置全屏显示

    }


    /** 跳转界面*/
    private fun intentActivity() {
        if (UserInfoManageUtil.isLogin()) {
            //是否报到(默认未报到)
            val partyReport = LKPrefUtil.getBoolean(UserInfoSharePreKey.PARTY_IS_REPORT, true)
            if (partyReport) {
                intentPartyReport()
            } else {
                intentAppMainActivity()
            }
        } else {
            intentLoginActivity()
        }
    }

    /**
     * 跳转到党员报到页面
     */
    private fun intentPartyReport() {
        val intent = Intent(this, AddPartyRegisterActivity::class.java)
        startActivity(intent)
        finish()
    }


    /** 跳转登录界面*/
    private fun intentLoginActivity() {
        val intent = Intent(this, LoginActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        startActivity(intent)
        finish()
    }


    /** 跳转应用主界面*/
    private fun intentAppMainActivity() {
        val intent = Intent(this, AppMainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        startActivity(intent)
        finish()
    }
}
